﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class PermissionInsertViewModel : NotificationObject
    {
        PermissionInsert gWindow;
        PermissionViewModel gParentViewModel;

        public PermissionInsertViewModel(PermissionInsert pWindow, PermissionViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;
        }

        private String permissionUID;
        public String PermissionUID
        {
            get
            {
                return permissionUID;
            }
            set
            {
                permissionUID = value;
                RaisePropertyChanged("PermissionUID");
            }
        }

        private String permissionName;
        public String PermissionName
        {
            get
            {
                return permissionName;
            }
            set
            {
                permissionName = value;
                RaisePropertyChanged("PermissionName");
            }
        }

        private String describe;
        public String Describe
        {
            get
            {
                return describe;
            }

            set
            {
                describe = value;
                RaisePropertyChanged("Describe");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WPermission> list = new List<WPermission>();
            WPermission item = new WPermission();
            item.PermissionUID = PermissionUID;
            item.PermissionName = PermissionName;
            item.Describe = Describe;
            list.Add(item);
            String postData = JSON.stringify(list);
            HttpResponse response = FridgeConnector.PostMessage(EStudy.Permission, EMethod.Insert, postData);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
